package com.hzit.controller;

import com.hzit.anno.RequiresPermissions;
import com.hzit.entity.Student;
import com.hzit.service.StudentService;
import com.hzit.utils.PageResult;
import com.hzit.utils.R;
import com.hzit.vo.StudentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 作者：李嘉浩
 * 时间：2024-08-21-17:15
 * 功能：
 */

@RestController
@RequestMapping("/api/student")
@RefreshScope
@Api("学生管理")

public class StudentController {

    @Autowired
    private StudentService studentService;

    @Value("${server.port}")
    private String port;

    @GetMapping("/list")
    @ApiOperation("学生列表")
    @RequiresPermissions(value = {"student:list"})
    public R list(){
        List<Student> list = studentService.list();
        return R.ok().data("port",port).data("list",list);
    }

    @GetMapping("/findById/{id}")
    @ApiOperation("id查询学生")
    public R findById(@PathVariable @ApiParam(value = "学生id",required = true) Integer id){
        Student byId = studentService.getById(id);
        return R.ok().data("port",port).data("byId",byId);
    }

    @PostMapping("/search/{page}/{pageSize}")
    @ApiOperation("条件分页查询学生")
    @RequiresPermissions(value = {"student:search"})
    public R search(@PathVariable Integer page,
                    @PathVariable Integer pageSize,
                    @RequestBody @ApiParam(value = "查询信息") StudentVO studentVO){
        PageResult<Student> search =studentService.search(page,pageSize,studentVO);
        return R.ok().data("list",search);
    }

    @PostMapping("/add")
    @ApiOperation("添加学生")
    @RequiresPermissions(value = {"student:add"})
    public R add(@RequestBody @ApiParam("需要添加的学生信息 可以不带sid") Student student){
        studentService.save(student);
        return R.ok().message("添加成功");
    }


    @PostMapping("/update")
    @ApiOperation("修改学生")
    @RequiresPermissions(value = {"student:update"})
    public R update(@RequestBody @ApiParam("需要修改的学生信息 必须要带sid") Student student){
        studentService.updateById(student);
        return R.ok().message("修改成功");
    }
}
